package net.vkits.common.sms;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import net.vkits.anjiamiao.domain.event.TransferSuccessEvent;
import net.vkits.common.exceptions.SmsException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by ethan-liu on 16/6/2.
 */
public class AlidayuSmsService implements SmsService {
    private static final Logger logger = LoggerFactory.getLogger(AlidayuSmsService.class);
    private static final SmsService INSTANCE = new AlidayuSmsService();

    private TaobaoClient client;

    private AlidayuSmsService() {
        this.client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23587088", "26daf2eca6a75eb3bf2d45e45b94d60e");
    }

    public static SmsService getInstance() {
        return INSTANCE;
    }

    @Override
    public void sendVerifyCode(String cellPhoneNum, String code) throws SmsException {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("安家喵");
        req.setSmsParamString("{\"code\":\"" + code + "\",\"product\":\"安家喵\"}");
        req.setRecNum(cellPhoneNum);
        req.setSmsTemplateCode("SMS_37675058");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.info(rsp.getBody());
        } catch (ApiException e) {
            logger.error("Error: ", e);
            throw new SmsException();
        }
    }

    @Override
    public void sendTransferSuccess(TransferSuccessEvent event) throws SmsException {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("安家喵");
        req.setSmsParamString("{\"rentAmount\":\""
                + event.getRentAmount()
                + "\",\"hasFee\":\""
                + (event.getHasFee()?"含水电杂费":"")
                + "\",\"landlord\":\""
                + event.getLandlord()
                + "\",\"bank\":\""
                + event.getBank()
                + "\",\"account\":\""
                + StringUtils.substring(event.getAccount(), -4)
                + "\",\"product\":\"安家喵\"}");
        req.setRecNum(event.getCellPhoneNum());
        req.setSmsTemplateCode("SMS_37630014");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.info(rsp.getBody());
        } catch (ApiException e) {
            logger.error("Error: ", e);
            throw new SmsException();
        }
    }

    @Override
    public void sendTransferFailure(String cellPhoneNum) throws SmsException {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("安家喵");
        req.setSmsParamString("{\"product\":\"安家喵\"}");
        req.setRecNum(cellPhoneNum);
        req.setSmsTemplateCode("SMS_37605040");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.info(rsp.getBody());
        } catch (ApiException e) {
            logger.error("Error: ", e);
            throw new SmsException();
        }
    }

    @Override
    public void sendRiskWarning(String cellPhoneNum) throws SmsException {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("安家喵");
        req.setSmsParamString("{\"product\":\"安家喵\"}");
        req.setRecNum(cellPhoneNum);
        req.setSmsTemplateCode("SMS_37580055");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.info(rsp.getBody());
        } catch (ApiException e) {
            logger.error("Error: ", e);
            throw new SmsException();
        }
    }

    @Override
    public void sendLandlordTransferSuccess(TransferSuccessEvent event) throws SmsException {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("安家喵");
        req.setSmsParamString("{\"rentAmount\":\""
                + event.getRentAmount()
                + "\",\"hasFee\":\""
                + (event.getHasFee()?"含水电杂费":"")
                + "\",\"customerName\":\""
                + event.getCustomerName()
                + "\",\"bank\":\""
                + event.getBank()
                + "\",\"account\":\""
                + StringUtils.substring(event.getAccount(), -4)
                + "\",\"product\":\"安家喵\"}");
        req.setRecNum(event.getCellPhoneNum());
        req.setSmsTemplateCode("SMS_37695087");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.info(rsp.getBody());
        } catch (ApiException e) {
            logger.error("Error: ", e);
            throw new SmsException();
        }
    }

    @Override
    public void sendRefundnotice(String cellPhoneNum, String rentAmount) throws SmsException {
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("安家喵");
        req.setSmsParamString("{\"rentAmount\":\""
                + rentAmount
                + "\",\"product\":\"安家喵\"}");
        req.setRecNum(cellPhoneNum);
        req.setSmsTemplateCode("SMS_37595021");
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            logger.info(rsp.getBody());
        } catch (ApiException e) {
            logger.error("Error: ", e);
            throw new SmsException();
        }
    }
}
